package com.study.zzedu.fragment.tab.notice;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.study.zzedu.R;
import com.study.zzedu.adapter.NoticeAdapter;
import com.study.zzedu.base.BaseFragment;
import com.study.zzedu.base.BaseView;
import com.study.zzedu.entity.NoticeItem;
import com.study.zzedu.fragment.notice.NoticeFragment;
import com.study.zzedu.ui.noticeinfodetail.NoticeInfoDetailActivity;
import com.study.zzedu.utils.AppConstant;
import com.study.zzedu.utils.ToastUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by kiros on 2020/9/5.
 * 课程公告页面
 */

public class CourseNoticeFragment extends BaseFragment<BaseView, CourseNoticePresenter> implements BaseView{

    private RecyclerView rl_notice;
    private RecyclerView.LayoutManager layoutManager;
    private NoticeAdapter noticeAdapter;
    private List<NoticeItem> noticeItems = new ArrayList<>();private TextView tv_wrong_count;

    @Override
    protected CourseNoticePresenter createPresenter() {
        return new CourseNoticePresenter();
    }

    @Override
    protected BaseView createView() {
        return this;
    }

    @Override
    protected int getLayoutID() {
        return R.layout.fragment_notice;
    }
    @Override
    protected void initView(View view) {
        rl_notice = view.findViewById(R.id.rl_notice);tv_wrong_count = view.findViewById(R.id.tv_wrong_count);
    }

    @Override
    protected void initData() {
        layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        noticeAdapter = new NoticeAdapter(getContext(), noticeItems);
        noticeAdapter.setOnItemClickListener(new NoticeAdapter.OnItemClickListner() {
            @Override
            public void onClick(int position) {
                //ToastUtil.showShortToast("posi:"+position);
                NoticeItem nItem = noticeItems.get(position);
                Intent intent = new Intent(getContext(), NoticeInfoDetailActivity.class);
                intent.putExtra("infoID",nItem.getId());
                intent.putExtra("from", AppConstant.COURSENOTICE);
                startActivityForResult(intent,position);
            }
        });
        rl_notice.setLayoutManager(layoutManager);
        rl_notice.setAdapter(noticeAdapter);

        getPresenter().getCourseNoticeInfo(getContext(),AppConstant.courseCode);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(noticeItems != null){
            //设置对应item为已读
            noticeItems.get(requestCode).setStatus(1);
            onChange.onChange(isHasUnRead(noticeItems));
            noticeAdapter.notifyItemChanged(requestCode);
        }
    }
    //true : 无未读  false：有未读
    public boolean isHasUnRead(List<NoticeItem> noticeItems){
        for (NoticeItem item : noticeItems){
            if(0 == item.getStatus())
                return false;
        }
        return true;
    }
    @Override
    protected void initListener() {
    }

    @Override
    public void getData(String data) {
        try {
            JSONObject jObject = new JSONObject(data);
            JSONArray jArray = jObject.getJSONArray("data");
            for (int i = 0;i<jArray.length();i++){
                noticeItems.add(new NoticeItem
                        (jArray.getJSONObject(i).getString("id"),jArray.getJSONObject(i).getInt("status"),
                                jArray.getJSONObject(i).getString("title"),
                                jArray.getJSONObject(i).getString("createDate")
                        ));
            }
            if(!(jArray.length()>0)){
                return;
            }else {
                noticeAdapter.notifyDataSetChanged();
                getActivity().findViewById(R.id.rl_default_notice).setVisibility(View.GONE);
            }
        } catch (Exception e) {
            e.toString();
        }
    }

    @Override
    public void showError(String error) {
        if (null != error && !TextUtils.isEmpty(error)) {
            ToastUtil.showShortToast(error);
        }
    }

    public static OnCountChange onChange;

    public interface OnCountChange{
        void onChange(boolean unRead);
    }
    public static void  setOnNoticeCountChange(OnCountChange change){
        onChange = change;
    }
}
